package com.atmilan.quartz.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.atmilan.quartz.mapper.ManifestProductMapper;
import com.atmilan.quartz.mapper.ManifestProductSkuMapper;
import com.atmilan.quartz.mapper.ManifestProductSkuSupplierMapper;
import com.atmilan.quartz.mapper.ProductMapper;
import com.atmilan.quartz.mapper.ProductSkuMapper;
import com.atmilan.quartz.mapper.ProductSkuSupplierMapper;
import com.atmilan.quartz.model.dto.buyer.ManifestDTO;
import com.atmilan.quartz.model.dto.buyer.MerchantProductSkuDTO;
import com.atmilan.quartz.model.dto.buyer.ProductDTO;
import com.atmilan.quartz.model.dto.buyer.ProductSkuDTO;
import com.atmilan.quartz.model.dto.buyer.SupplierPriceDTO;
import com.atmilan.quartz.model.entity.buyer.ManifestProduct;
import com.atmilan.quartz.model.entity.buyer.ManifestProductExample;
import com.atmilan.quartz.model.entity.buyer.ManifestProductSku;
import com.atmilan.quartz.model.entity.buyer.ManifestProductSkuExample;
import com.atmilan.quartz.model.entity.buyer.ManifestProductSkuSupplier;
import com.atmilan.quartz.model.entity.buyer.ManifestProductSkuSupplierExample;
import com.atmilan.quartz.model.entity.buyer.Product;
import com.atmilan.quartz.model.entity.buyer.ProductExample;
import com.atmilan.quartz.model.entity.buyer.ProductSku;
import com.atmilan.quartz.model.entity.buyer.ProductSkuSupplier;
import com.atmilan.quartz.model.entity.buyer.ProductSkuSupplierExample;
import com.atmilan.quartz.service.IErpProductUpdateService;
import com.atmilan.quartz.service.IManifestService;
import com.atmilan.quartz.service.IMerchantSupplierService;
import com.atmilan.quartz.service.IProductService;
import com.atmilan.quartz.service.IProductSkuService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class ErpProductUpdateServiceImpl implements IErpProductUpdateService {
    @Autowired
    ProductSkuMapper productSkuMapper;
    @Autowired
    ProductSkuSupplierMapper productSkuSupplierMapper;
    @Autowired
    IProductService productService;
    @Autowired
    ProductMapper productMapper;
    @Autowired
    ManifestProductSkuSupplierMapper manifestProductSkuSupplierMapper;
    @Autowired
    IManifestService manifestService;
    @Autowired
    IMerchantSupplierService merchantSupplierService;
    @Autowired
    ManifestProductSkuMapper manifestProductSkuMapper;
    @Autowired
    IProductSkuService productSkuService;
    @Autowired
    ManifestProductMapper manifestProductMapper;

    @Override
    public boolean updateProductInfo(Long supplierId, Long skuId, String productCode, Integer stock, Double price) {

        try {
            ProductExample productExample = new ProductExample();
            productExample.createCriteria().andProductCodeEqualTo(productCode);
            List<Product> list = productMapper.selectByExample(productExample);
            if (list.size() > 0) {
                Product product = list.get(0);
                //要修改的数据是价格和库存  stock price
                ProductSkuSupplierExample productSkuSupplierExample = new ProductSkuSupplierExample();
                productSkuSupplierExample.createCriteria().andSkuIdEqualTo(skuId).andSupplierIdEqualTo(supplierId).andSupplierIsUpdatePriceEqualTo(0).// 0没有修改过 1 修改过
                        andProductIdEqualTo(product.getId());
                //  产品 skuId 供应商 ID 找出要修改的 数据
                ProductSkuSupplier productSkuSupplier = new ProductSkuSupplier();
                productSkuSupplier.setPrice(new BigDecimal(price));
                productSkuSupplier.setStock(stock);
                productSkuSupplierMapper.updateByExampleSelective(productSkuSupplier, productSkuSupplierExample);
                // 修改以后 货单里面的价格重新计算

                //查找 货单里面 这个商品下 这个sku 的数据 进行 重新计算
                ManifestProductSkuSupplierExample manifestProductSkuSupplierExample = new ManifestProductSkuSupplierExample();
                manifestProductSkuSupplierExample.createCriteria().andSkuIdEqualTo(skuId).andProductIdEqualTo(product.getId());

                List<ManifestProductSkuSupplier> listManifestProductSkuSupplier = manifestProductSkuSupplierMapper.selectByExample(manifestProductSkuSupplierExample);
                for (ManifestProductSkuSupplier manifestProductSkuSupplier : listManifestProductSkuSupplier) {
                    // 判断供应商是否修改过这个价格 如果修改过这个价格 就不做处理了
                    ProductSkuSupplier productSkuSupplier1 = productSkuSupplierMapper.selectByPrimaryKey(manifestProductSkuSupplier.getSkuId());
                    if (productSkuSupplier1.getSupplierIsUpdatePrice() == 0) {
                        //更新他们的价格
                        Long merchantId = manifestProductSkuSupplier.getMerchantId();
                        Long manifestId = manifestProductSkuSupplier.getManifestId();
                        //根据货单ID 查找货单的加价率
                        ManifestDTO manifest = manifestService.getManifestById(manifestId);

                        List<MerchantProductSkuDTO> listSupplier = merchantSupplierService.getByMerchantIdAndProductId(merchantId, product.getId());
                        MerchantProductSkuDTO merchantProductSkuDTO = getById(listSupplier, skuId);
                        List<SupplierPriceDTO> list1 = merchantProductSkuDTO.getSupplierList();
                        //获取他的最新价格
                        for (SupplierPriceDTO supplierPriceDTO : list1) {
                            if (supplierPriceDTO.getSupplierId() == supplierId) {
                                //这个供应商对 对这个 产品 对这个 sku 对这个 商户的报价 + 货单的加价率 就是货单的价格了
                                BigDecimal manifestProductSkuPrice = supplierPriceDTO.getPrice().add(supplierPriceDTO.getPrice().multiply(manifest.getPriceIncreaseRate())); //价格
                                manifestProductSkuSupplier.setSupplierPrice(supplierPriceDTO.getPrice());
                                //更新报价
                                manifestProductSkuSupplierMapper.updateByPrimaryKeySelective(manifestProductSkuSupplier);

                                //更新货单SKU价
                                ManifestProductSkuExample manifestProductSkuExample = new ManifestProductSkuExample();
                                ManifestProductSku manifestProductSku = new ManifestProductSku();
                                manifestProductSku.setManifestId(manifestId);
                                manifestProductSku.setProductId(product.getId());
                                manifestProductSku.setSkuId(skuId);
                                manifestProductSku.setMerchantId(merchantId);
                                manifestProductSku.setSupplierPrice(manifestProductSkuPrice);
                                manifestProductSku.setPrice(manifestProductSkuPrice);
                                manifestProductSku.setUpdateTime(new Date());
                                manifestProductSkuMapper.updateByExample(manifestProductSku, manifestProductSkuExample);
                            }
                        }
                    }

                }

                return true;

            }
        } catch (Exception e) {
            log.info(e.getMessage());
            return false;
        }
        return false;

    }

    //0: 下架  1: 上架
    @Override
    public boolean updateProductInfo(String productCode, Integer onSaleStatus) {
        ProductExample productExample =new ProductExample();
        productExample.createCriteria().andProductCodeEqualTo(productCode);
        List<Product> list=  productMapper.selectByExample(productExample);
        if(list.size()>0){
            Product product=    list.get(0);
            //修改产品本身
            product.setOnSaleStatus(onSaleStatus);
            productMapper.updateByPrimaryKeySelective(product);
            if(product.getOnSaleStatus()!=null){


                //修改 货单产品 状态
                ManifestProductExample manifestProductExample=new ManifestProductExample();
                manifestProductExample.createCriteria().andProductIdEqualTo(product.getId()).andOnSaleStatusEqualTo(1);
                ManifestProduct manifestProduct=new ManifestProduct();
                manifestProduct.setOnSaleStatus(onSaleStatus);
                manifestProductMapper.updateByExampleSelective(manifestProduct,manifestProductExample);
                return true;
            }

        }
        return false;
    }

    @Override
    public boolean updateSkuSaleStatus(Long skuId, Integer onSaleStatus, Long supplierId) {
        ProductSkuDTO cccc = productSkuService.getProductSkuById(skuId);
        cccc.setOnSaleStatus(onSaleStatus);
        Boolean aa = productSkuService.updateProductSku(cccc);

        //同时修改货单上下架状态
        ManifestProductSkuSupplierExample manifestProductSkuSupplierExample = new ManifestProductSkuSupplierExample();
        manifestProductSkuSupplierExample.createCriteria().andSkuIdEqualTo(skuId);
        List<ManifestProductSkuSupplier> manifestProductSkuSuppliers = manifestProductSkuSupplierMapper.selectByExample(manifestProductSkuSupplierExample);
        for (ManifestProductSkuSupplier m : manifestProductSkuSuppliers) {
            //如果当前货单 是下架状态 则不更新 如果是上架状态 则更新
            if (m.getIsSale() == 0) {

            } else {
                m.setIsSale(onSaleStatus);
                manifestProductSkuSupplierMapper.updateByPrimaryKey(m);
            }

        }
        return false;
    }

    public MerchantProductSkuDTO getById(List<MerchantProductSkuDTO> list, Long skuId) {

        for (MerchantProductSkuDTO productSkuDTO : list) {
            if (productSkuDTO.getSkuId() == skuId) {
                return productSkuDTO;
            }
        }
        return null;
    }

}
